/*
* ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
* Without permission, no one has the right to obtain, disclose or use this software in any way.
*/

package ecarx.xsf.notificationcenter.utils;

import android.app.NotificationChannel;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import zwdroid.top.hidesdk.ActivityManagerWrapper28;
import zwdroid.top.hidesdk.NotificationManagerWrapper;

import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
import static android.app.NotificationManager.IMPORTANCE_NONE;

/**
 * Date    :  2019-06-25 16:38
 * Content :  通知相关的后台工具类(INotificationManager)
 */
public class NotificationBackend {

    private static final String TAG = "NotificationBackend";

    /**
     * 屏蔽一个应用的所有通知，适用于4.3+
     *
     * @param pkg     包名
     */
    public static boolean blockNotificationForPackage(Context context, String pkg) {
        return enableNotificationForPackage(context, pkg, false);
    }


    /**
     * 屏蔽一个应用中的单个Channel的通知
     *
     * @param pkg       包名
     * @param channelId 要屏蔽的Channel的id
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void blockNotificationForChannel(Context context, String pkg, String channelId) {
        enableNotificationForChannel(context, pkg, channelId, false);
    }

    /**
     * 是否开启通知
     *
     */
    public static boolean enableNotificationForPackage(Context context, String pkg, boolean enable) {
        final int uid = getUid(context, pkg);
        return NotificationManagerWrapper.setNotificationsEnabledForPackage(pkg, uid, enable);
    }

    /**
     * 是否屏蔽一个应用中的单个Channel的通知
     *
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    public static void enableNotificationForChannel(Context context, String pkg, String channelId, boolean enable) {
        final int uid = getUid(context, pkg);
        NotificationChannel channel = NotificationManagerWrapper.getChannel(pkg, uid, channelId);
        if (channel == null) {
            Log.e(TAG, "NotificationChannel is null with channelId=" + channelId);
            return;
        }
        int importance = enable ? IMPORTANCE_DEFAULT : IMPORTANCE_NONE;
        changeImportanceOfChannel(pkg, uid, channel, importance);
    }



    /**
     * 修改一个Channel的importance
     *
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private static void changeImportanceOfChannel(String pkg, int uid, NotificationChannel channel, int importance) {
        channel.setImportance(importance);
        NotificationManagerWrapper.lockFields(channel);
        NotificationManagerWrapper.updateChannel(pkg, uid, channel);
    }

    /**
     * 获取当前的uid
     *
     */
    private static int getUid(Context context, String pkg) {
        PackageManager packageManager = context.getPackageManager();
        int uid = ActivityManagerWrapper28.getCurrentUser();

        try {
            ApplicationInfo applicationInfo = packageManager.getApplicationInfo(pkg, 0);
            uid = applicationInfo.uid;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return uid;
    }


}
